package com.zzg.mybatis.generator.controller;

import com.zzg.mybatis.generator.model.UITableColumnVO;
import com.zzg.mybatis.generator.model.UITableQueryColumnVO;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import org.mybatis.generator.config.ColumnOverride;
import org.mybatis.generator.config.IgnoredColumn;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by Owen on 6/20/16.
 */
public class SelectTableQueryColumnController extends BaseFXController {

    @FXML
    private TableView<UITableQueryColumnVO> columnListView;
    @FXML
    private TableColumn<UITableQueryColumnVO, Boolean> checkedColumn;
    @FXML
    private TableColumn<UITableQueryColumnVO, String> columnNameColumn;
    @FXML
    private TableColumn<UITableQueryColumnVO, String> jdbcTypeColumn;
    @FXML
    private TableColumn<UITableQueryColumnVO, Boolean> ruzzyQueryColumn;
    @FXML
    private TableColumn<UITableQueryColumnVO, Boolean> rangeQueryColumn;
    @FXML
    private TableColumn<UITableQueryColumnVO, Boolean> orderByColumn;

    private MainUIController mainUIController;

    private String tableName;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // cellvaluefactory
        checkedColumn.setCellValueFactory(new PropertyValueFactory<>("checked"));
        columnNameColumn.setCellValueFactory(new PropertyValueFactory<>("columnName"));
        jdbcTypeColumn.setCellValueFactory(new PropertyValueFactory<>("jdbcType"));
        ruzzyQueryColumn.setCellValueFactory(new PropertyValueFactory<>("ruzzyQuery"));
        rangeQueryColumn.setCellValueFactory(new PropertyValueFactory<>("rangeQuery"));
        orderByColumn.setCellValueFactory(new PropertyValueFactory<>("orderBy"));
        // Cell Factory that customize how the cell should render
        checkedColumn.setCellFactory(CheckBoxTableCell.forTableColumn(checkedColumn));
        rangeQueryColumn.setCellFactory(CheckBoxTableCell.forTableColumn(rangeQueryColumn));
        ruzzyQueryColumn.setCellFactory(CheckBoxTableCell.forTableColumn(ruzzyQueryColumn));
        orderByColumn.setCellFactory(CheckBoxTableCell.forTableColumn(orderByColumn));
    }

    @FXML
    public void ok() {
        ObservableList<UITableQueryColumnVO> items = columnListView.getItems();
        if (items != null && items.size() > 0) {
            List<UITableQueryColumnVO> queryColumns = new ArrayList<>();
            items.forEach(item -> {
                if (item.getChecked() || item.isRuzzyQuery() || item.isRangeQuery() || item.isOrderBy()) {
                    queryColumns.add(item);
                }
            });
            mainUIController.setQueryColumnVOS(queryColumns);
        }
        getDialogStage().close();
    }

    @FXML
    public void cancel() {
        getDialogStage().close();
    }

    public void setColumnList(ObservableList<UITableQueryColumnVO> columns) {
        columnListView.setItems(columns);
    }

    public void setMainUIController(MainUIController mainUIController) {
        this.mainUIController = mainUIController;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @FXML
    public void reset(ActionEvent actionEvent) {
        mainUIController.setQueryColumnVOS(null);
    }
}
